package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.model.bo.MallBO;
import com.cskaoyan.model.vo.ExpressVO;
import com.cskaoyan.model.vo.MallVO;
import com.cskaoyan.model.vo.OrderVO;
import com.cskaoyan.model.vo.WxVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MallServiceImpl implements MallService {

    @Autowired
    MallMapper mallMapper;
    @Autowired
    ExpressMapper expressMapper;
    @Autowired
    OrderConfigMapper orderConfigMapper;
    @Override
    public MallVO selectByname() {
        MallExample example = new MallExample();
        MallExample.Criteria criteria = example.createCriteria();
        Mall res =  mallMapper.selectByPrimaryKey(1);
        MallVO mallVO = new MallVO();
        mallVO.setCskaoyan_mall_mall_name(res.getName());
        mallVO.setCskaoyan_mall_mall_address(res.getAddress());
        mallVO.setCskaoyan_mall_mall_phone(res.getPhone());
        mallVO.setCskaoyan_mall_mall_qq(res.getQq());
        return mallVO;
    }

    @Override
    public int update(MallBO mallBO) {
        MallExample example = new MallExample();
        Mall mall = new Mall();
        mall.setId(1);
        mall.setAddress(mallBO.getCskaoyan_mall_mall_address());
        mall.setName(mallBO.getCskaoyan_mall_mall_name());
        mall.setPhone(mallBO.getCskaoyan_mall_mall_phone());
        mall.setQq(mallBO.getCskaoyan_mall_mall_qq());
        int res = mallMapper.updateByPrimaryKey(mall);
        return res;
    }

    @Override
    public ExpressVO select() {
        Express express = expressMapper.selectByPrimaryKey(1);
        ExpressVO expressVO = new ExpressVO();
        expressVO.setCskaoyan_mall_express_freight_min(express.getFreightMin());
        expressVO.setCskaoyan_mall_express_freight_value(express.getFreightValue());
        return expressVO;
    }

    @Override
    public OrderVO selectorder() {
        OrderConfig order = orderConfigMapper.selectByPrimaryKey(1);
        OrderVO orderVO = new OrderVO();
        orderVO.setCskaoyan_mall_order_comment(order.getComment());
        orderVO.setCskaoyan_mall_order_unconfirm(order.getUnconfirm());
        orderVO.setCskaoyan_mall_order_unpaid(order.getUnconfirm());
        return orderVO;
    }

    @Autowired
    WxMapper wxMapper;
    @Override
    public WxVO selectWx() {
        Wx wx = new Wx();
        wx = wxMapper.selectByPrimaryKey(1);
        WxVO wxVO = new WxVO();
        wxVO.setCskaoyan_mall_wx_share(wx.getIndexShare());
        wxVO.setCskaoyan_mall_wx_catlog_goods(wx.getCatlogGoods());
        wxVO.setCskaoyan_mall_wx_catlog_list(wx.getCatlogList());
        wxVO.setCskaoyan_mall_wx_index_brand(wx.getIndexBrand());
        wxVO.setCskaoyan_mall_wx_index_hot(wx.getIndexHot());
        wxVO.setCskaoyan_mall_wx_index_new(wx.getIndexNew());
        //数据表少了一个属性，用一个固定的
        wxVO.setCskaoyan_mall_wx_index_topic("0");
        return wxVO;
    }


    @Override
    public int update_express(ExpressVO expressVO) {
        Express express = new Express();
        express.setId(1);
        express.setFreightMin(expressVO.getCskaoyan_mall_express_freight_min());
        express.setFreightValue(expressVO.getCskaoyan_mall_express_freight_value());
        int res = expressMapper.updateByPrimaryKey(express);
        if(res == 1){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public int update_order(OrderVO orderVO) {
        OrderConfig orderConfig = new OrderConfig();
        orderConfig.setId(1);
        orderConfig.setComment(orderVO.getCskaoyan_mall_order_comment());
        orderConfig.setUnconfirm(orderVO.getCskaoyan_mall_order_unconfirm());
        orderConfig.setUnpaid(orderVO.getCskaoyan_mall_order_unpaid());
        int res = orderConfigMapper.updateByPrimaryKey(orderConfig);
        if(res == 1){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public int update_mall(MallVO mallVO) {
        Mall mall = new Mall();
        mall.setId(1);
        mall.setQq(mallVO.getCskaoyan_mall_mall_qq());
        mall.setPhone(mallVO.getCskaoyan_mall_mall_phone());
        mall.setName(mallVO.getCskaoyan_mall_mall_name());
        mall.setAddress(mallVO.getCskaoyan_mall_mall_address());
        int res = mallMapper.updateByPrimaryKey(mall);
        if(res == 1){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public int update_wx(WxVO wxVO) {
        Wx wx = new Wx();
        wx.setId(1);
        wx.setCatlogGoods(wxVO.getCskaoyan_mall_wx_catlog_goods());
        wx.setCatlogList(wxVO.getCskaoyan_mall_wx_catlog_list());
        wx.setIndexBrand(wxVO.getCskaoyan_mall_wx_index_brand());
        wx.setIndexHot(wxVO.getCskaoyan_mall_wx_index_hot());
        wx.setIndexNew(wxVO.getCskaoyan_mall_wx_index_new());
        wx.setIndexShare("true");
        int res = wxMapper.updateByPrimaryKey(wx);
        if(res == 1){
            return 1;
        }else{
            return 0;
        }
    }
}
